import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { FactoryService } from '../../../../providers/factory-service';
import { DictionaryService } from '../../../../providers/dictionary-service';
import { AuthService } from '../../../../providers/auth-service';
import { FactoryDetailPage } from '../detail/detail'




@Component({
  selector: 'page-factory',
  templateUrl: 'list.html'
})

export class FactoryListPage {
  currentUser:any
  title=''
  factoryList=[]
  businessFilter=''
  MainBusiness=[]
  constructor(
    public navCtrl: NavController,
    private auth: AuthService,
    private factory: FactoryService,
    private dict: DictionaryService
    ) {
    this.currentUser  = auth.getUserInfo()
    this.title = this.currentUser.factory.type ===0?'加工厂':'服装厂'
    let self = this
    factory.getByType(this.currentUser.factory.type === 0?1:0).then(data =>{
      data.forEach(item => {
        item.main_business_name = dict.getLableByTypeAndvalue('business', item.main_business)
      })
      self.factoryList = data
    })
    this.MainBusiness = dict.getByType('business')
  }
  public toFactoryDetail(factory) {
    this.navCtrl.push(FactoryDetailPage, {
      factory: factory
    })
  }
  public filter() {
    return this.factoryList.filter(item => {
      if (this.businessFilter===''||!this.businessFilter || this.businessFilter.length <=0) {
        return true
      }
      return this.businessFilter.indexOf(item.main_business + '') >=0;
    });
  }
}
